package Servlet;

import DAO.ComplaintDAO;
import DAO.DAOFactory;
import javabean.Staff;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/assignComplaint")
public class assignComplaintServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType( "text/html; charset=UTF-8" );
        HttpSession session = req.getSession();
        Staff loginStaff = (Staff) session.getAttribute("loginStaff");
        int ComplaintID = Integer.parseInt(req.getParameter("ComplaintID"));
        ComplaintDAO complaintDAO = (ComplaintDAO) DAOFactory.getComplaintDAOInstance();
        PrintWriter out = resp.getWriter();
        try {
            if (complaintDAO.addComplaint(ComplaintID, loginStaff.getStaffID())) {
                //提示分配成功并点击确定后刷新页面
                out.print("<script>alert('分配成功!');window.location.href='UnassignedComplaint'</script>");
            } else {
                out.print("<script>alert('分配失败!');window.location.href='UnassignedComplaint'</script>");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}